/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components["common.ProgressBar"]={
	Init:function()
	{
		if (this.P.Max == undefined) {
			this.P.Max = 100;
		}
		if (this.P.Min == undefined) {
			this.P.Min = 0;
		}
		if (this.P.Value == undefined) {
			this.P.Value = 0;
		}
		this.Update();
		return this;
	},
	SetMax:function(value)
	{
		var v = parseFloat(value);
		if (!isNaN(v))
		{
			if (v < this.P.Min)
			{
				v = this.P.Min;
			}
			this.P.Max = v;
			this.Update();
		}
		return this;
	},
	GetMax:function()
	{
		return this.P.Max;
	},
	SetMin:function(value)
	{
		var v = parseFloat(value);
		if (!isNaN(v))
		{
			if (v > this.P.Max)
			{
				v = this.P.Max;
			}
			this.P.Min = v;
			this.Update();
		}
		return this;
	},
	GetMin:function()
	{
		return this.P.Min;
	},
	SetValue:function(value)
	{
		var v = parseFloat(value);
		if (!isNaN(v)) {
			var min = this.P.Min;
			var max = this.P.Max;
			if (v<min)
			{v = min;}
			if (v>max)
			{v = max;}
			this.P.Value = v;
			this.Update();
		}
		return this;
	},
	GetValue:function()
	{
		return this.P.Value;
	},
	Update:function()
	{
		var min = this.P.Min;
		var max = this.P.Max;
		var v = this.P.Value;
		v = ((v - min) / (max - min));
		if (!isNaN(v))
		{
			this.C.progress[0].style.width = (v*100).toFixed(0) + '%';
			this.C.progress_text[0].innerHTML = (v*100).toFixed(0) + '%';
		}
	}
};

// -----------------------------------------------
// Compilation INFO
// -----------------------------------------------
jsForms.CreateCompileInfoByObject(
{
	Name: "common.ProgressBar",
	Properties: {
		Max: {
			Type: 'float',
			Access: 'getset'
		},
		Min: {
			Type: 'float',
			Access: 'getset'
		},
		Value: {
			Type: 'float',
			Access: 'getset'
		}
	},
	Events: {}
});
// -----------------------------------------------
